package robocup.component.actionfactories;

import robocup.component.actions.TurnNeckAction;
import robocup.component.geometry.Vektor;
import robocup.component.worldobjects.Player;
import robocup.component.worldobjects.Player.RELATIVE;

/**
 * A factory for turn-neck actions. Thsi provides some methods to easiliy select
 * the desired looking direction. Per default, it uses the
 * <code>NeckRotator</code>, which determines the next view-angle by some
 * sort of importance for certain objects/distances, specified independently for
 * each state.
 */
public class TurnNeckActionFactory {

  /**
   * Returns a turn neck action to watch the given point, if possible.
   * 
   * @param p
   * @param point
   * @return watch to point if possible
   */
  public static TurnNeckAction watchPoint(final Player p,
      final Vektor point) {
    
    final double maxAngle = p.getAngleRel(point, RELATIVE.TO_BODY);
    double ang = p.getAngleRel(point, RELATIVE.TO_HEAD);
    double diff=0.0;
    if (Math.abs(maxAngle) <= 90) {
      return new TurnNeckAction(ang);
    } else if (Math.abs(maxAngle) <= 120){
        if(maxAngle > 0){
          diff = maxAngle - 90.0;
        }else{
          diff = maxAngle + 90.0;
        }
        return new TurnNeckAction(ang - diff);
    }
    return null;
  }

}
